import { useState, useEffect } from 'react'
function useRequest(url, initValue = null) {
    const [data, setData] = useState(initValue)
    const [loading, setLoading] = useState(true)
    const [error, setError] = useState(null)
    useEffect(() => {
        setLoading(true)
        setTimeout(() => {
            fetch(url)
                .then(res => res.json())
                .then(res => {
                    setData(res);
                    setLoading(false)
                })
                .catch(err => setError(err));
        }, 1000);
        // 1.请求API,将数据放到data中
        // 2.在请求开始前将loading设置为true，请求结束后设置为false
        // 3.请求失败时将error设置为错误信息
    }, [url])
    return { data, loading, error }
}

export default useRequest